package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.mapper.MenuMapper;
import cn.tedu.fruitshop.pojo.dto.MenuAddNewDTO;
import cn.tedu.fruitshop.pojo.vo.MenuListItemVO;
import cn.tedu.fruitshop.service.MenuService;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author www
 * @create 2022-11-05 17:52
 */
@Slf4j
@Valid
@Api(tags = "06.菜单管理模块")
@RestController
@RequestMapping("/menus")
public class MenuController {

    @Autowired
    MenuService menuService;
    @Autowired
    MenuMapper mapper;

    //http:localhost:8080/menus/add-new
    @ApiOperation("添加菜单")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    public JsonResult insert(@Valid MenuAddNewDTO menuAddNewDTO) {
        log.debug("开始处理【添加菜单的请求】，参数：{}",menuAddNewDTO);
        menuService.add(menuAddNewDTO);
        return JsonResult.ok();
    }

    //http:localhost:8080/menus/title/delete
    @ApiOperation("根据标题删除菜单")
    @ApiOperationSupport(order = 201)
    @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "String")
    @GetMapping("/{title}/delete")
    public JsonResult<Void> deleteByTitle(@NotNull(message = "标题不能为空") @PathVariable String title) {
        log.debug("开始处理【根据标题删除菜单】的请求，参数：{}", title);
        menuService.deleteByTitle(title);
        return JsonResult.ok();
    }

    // http://localhost:8080/menus
    @ApiOperation("查询菜单列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("")
    public JsonResult<List<MenuListItemVO>> list() {
        log.debug("开始处理【查询菜单列表】的请求，无参数");
        List<MenuListItemVO> list = menuService.list();
        return JsonResult.ok(list);
    }

//    @RequestMapping("/select")
//    public List<MenuListItemVO> select(){
//        return mapper.list();
//    }

}
